package coollemon.dataBase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Connect {

	public static Connection getConnection(String user,String password,String database,String port) {
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost:"+port+"/"+database;
		try {
			Class.forName(driver);
		} catch (Exception e) {
			System.out.println("Unable to load drivers:" + driver);
		}
		try {
			Connection con = DriverManager.getConnection(url, user, password);
			if (!con.isClosed()) {
				System.out.println("Connect DataBase successfully!");
				return con;
			}
		} catch (SQLException ee) {
			System.out.println("Connect DataBase failed!" + ee.getMessage());
		}
		return null;
	}
	
	public static void main(String[] args) {
		Connection con = Connect.getConnection("root","tsinghua","coollemon","3308");
	}

}
